package com.jessechanrui.template.database.mybatisplus.assembler;

import com.jessechanrui.template.database.mybatisplus.dto.UserDTO;
import com.jessechanrui.template.database.mybatisplus.entity.UserDO;
import com.jessechanrui.template.database.mybatisplus.entity.enums.SexEnum;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

@Mapper
public interface UserAssembler {
    UserAssembler INSTANCE = Mappers.getMapper(UserAssembler.class);
    @Mappings({
            @Mapping(source="id", target = "userId"),
            @Mapping(source="nickname", target = "userName"),
            @Mapping(source="sex", target = "userSex"),
            @Mapping(source="version", target = "userVersion")
    })
    UserDTO toDTO(UserDO userDO);

    @Mappings({
            @Mapping(source = "userId", target="id"),
            @Mapping(source = "userName", target="nickname"),
            @Mapping(source = "userSex", target="sex"),
            @Mapping(source = "userVersion", target="version")
    })
    UserDO toDO(UserDTO userDTO);

    //枚举类字段转换
    default SexEnum customConveter(String name){
        for (SexEnum  ge: SexEnum.values()){
            if (ge.getName().equals(name)){
                return ge;
            }
        }
        return null;
    }
}
